//const baseUrl = "http://www.itoloan.com"; //服务器地址
var  str = '1.gjvg03loo10qqatoplwdwh6zcln1dfdenawk05ljwtsz2z3oeafld4urf7qmsvoa';
localStorage.setItem('user', str);
var token = localStorage.getItem("user");//获取token

$(function() {
	
    var myChart = echarts.init(document.querySelector("#chart-list01"));
    var user = localStorage.getItem('user');
// 点击更多跳转至买卖列表
    $('.quo-more-span').click(function() {
        window.location = 'buyandsellList/';
    })
// 我要卖出
    $('.quo-need-sell').click(function() {
        window.location = 'sell/'
    });
// 跳转至交易记录
    $('.transaction-record').click(function() {
        console.log('交易记录');
        window.location = 'transaction/';
    });
    //获取图表数据
    $.ajax({
        headers: {
            "Content-type": "application/json;charset=UTF-8",
            "token": user
        },
        type: "post",
        url: baseUrl + "/api/trade/deal_line",
        async: false,
        success: function(res) {
            var list, xData, yData,code;
            xData = [];
            yData = [];
            list = res.data.deal_line;
            code = res.data.had_cre;
            for(var i = 0; i < list.length; i++) {
                xData.push(list[i].date);
                yData.push(list[i].price);
            }
            var option = {
                title: {
                    text: "", //图表标题
                    show: false
                },
                tooltip: {
                    trigger: "axis",
                    show: false
                },
                legend: {
                    data: [], //有几条线对应几组数据，每条线的具体文字,图例名
                    show: true
                },
                grid: { //图表距离边框的距离
                    left: "1%",
                    right: "1%",
                    bottom: "1%",
                    //containLabel: true,
                    borderWidth: 0, //去掉右上边框
                    show: true
                },
                //工具框
                toolbox: {
                    feature: {
                        saveAsImage: {}
                    }
                },
                //x轴信息样式
                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    data: xData,
                    //坐标轴颜色
                    axisLine: {
                        lineStyle: {
                            color: '#eaeaea',
                            borderWidth: 1
                        },
                        // show : false
                    },
                    //x轴文字旋转
                    axisLabel: {
                        show: true,
                        rotate: 0,
                        interval: 0
                    },
                    splitLine: {
                        show: false //去掉网格线
                    },
                    // splitNumber: 0.02,
                    axisTick: {
                        show: false
                    }
                },
                yAxis: {
                    type: "value",
                    axisLine: {
                        lineStyle: {
                            color: '#eaeaea',
                            borderWidth: 1
                        }
                    },
                    axisTick: {
                        show: false
                    },
                    splitLine: {
                        show: false
                    },
                    borderWidth: 0,
                    data: yData,
                    min: 0,
                    max: 20,
                    interval: 0.5
                },
                series: [{
                    type: "line",
                    data: yData,
                    lineStyle: {
                        color: "#FD7D89"
                    },
                    itemStyle: {
                        color: "#FD7D89"

                    },
                    //			symbolSize: 3,

                }]
            };
            if(option && typeof option === "object") {
                myChart.setOption(option, true);
            }
            $('.quo-chart-code').html(code)

        }
    });

    //获取列表数据
    $.ajax({
        headers: {
            "Content-Type": "application/x-www-form-urlencoded",
            "token": user //此处放置请求到的用户token
        },
        type: "post",
        url: baseUrl + "/api/trade/dealList",
        data: {
            'page': 1,
        },
        async: false,
        success: function(res) {

            var list = res.data.deal_list;
            if(list.length > 0) {
                for(var i = 0; i < list.length; i++) {
                    var imgStr = "<img src=\"" + list[i].avatar + "\" />";
                    str =
                        "<li>\n" +
                        "<div class=\"quo-user-con-img\">\n" +
                        imgStr +
                        "</div>\n" +
                        "<div class=\"quo-user-con-price\">\n" +
                        "<span>￥</span>\n" +
                        "<span>" + list[i].unit_price + "</span>\n" +
                        "</div>\n" +
                        "<div class=\"quo-user-con-shengYu\">\n" +
                        " <span class=\"quo-shengYu\">" + list[i].rest_amount + "</span>\n" +
                        "<span>/</span>\n" +
                        "<span class=\"quo-sell\">" + list[i].total_amount + "</span>\n" +
                        "</div>\n" +
                        "</li>"

                    $(".quo-user-con ul").append(str);

                }
                //页面跳转
                $('.quo-user-con ul li').each(function(index) {

                    $(this).click(function() {

                        // window.location = 'buy.html?id=' + list[index].deal_id;
                        window.location = 'buy/' + list[index].deal_id;
                    })

                })
              }else{
                $('.quo-user-con ul').html('暂无数据');


            }

        }

    })
});